package com.cskaoyan.service.adminservice.impl;

import com.cskaoyan.bean.po.MarketCategory;
import com.cskaoyan.bean.po.MarketCategoryExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.service.adminservice.CategoryService;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author:J
 * @datetime 2022/12/30 5:17
 * @description: TODO:
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    @Autowired
    CategoryMapper categoryMapper;

    @Override
   public CommonData<MarketCategory> list(BasePageInfo info) {
        //根据id查询数据，id不会为空，不校验。
        //PageHelper.startPage(info.getPage(),info.getLimit());


        MarketCategoryExample example=new MarketCategoryExample();

        //排序
        //example.setOrderByClause(info.getSort()+""+info.getOrder());

        //建查询标准(条件)
        MarketCategoryExample.Criteria criteria=example.createCriteria();
        //criteria.andIdIsNotNull();
        criteria.andDeletedEqualTo(false);

        //所有数据
        List<MarketCategory> list=marketCategoryMapper.selectByExample(example);
        //children表
        List<MarketCategory> listCategory =null;
        List<MarketCategory> listCategoryChildren=null;

        for (MarketCategory marketCategory : list) {
            listCategory= categoryMapper.selectByPid(marketCategory.getId());
            for (MarketCategory marketCategory1 : listCategory) {
                listCategoryChildren=  categoryMapper.selectByPid(marketCategory1.getId());
                marketCategory1.setChildren(listCategoryChildren);
            }
            marketCategory.setChildren(listCategory);
        }


        PageInfo<MarketCategory> pageInfo=new PageInfo<>(list);

//        pageInfo.setList(list);

        return CommonData.data(pageInfo);

        //获得分页信息
        //data:limit,list,page,pages,total

    }

    /**
     * id，name,keywords,desc,iconUrl,level,picUrl
     * 新增商品目录
     * @param marketCategory
     * @return
     */
    @Override
    public BaseRespVo create(MarketCategory marketCategory) {
        BaseRespVo baseRespVo=new BaseRespVo<>();

        //类目名称要求非空，，，
        if(!StringUtil.isEmpty(marketCategory.getName())){
           // MarketCategory newMarketCategory=new MarketCategory(marketCategory.getName(),marketCategory.getId(),
                 //   marketCategory.getDesc(),marketCategory.getIconUrl(),marketCategory.getPicUrl(),
                 //   marketCategory.getKeywords(),marketCategory.getLevel());
            MarketCategory newMarketCategory=new MarketCategory();

            newMarketCategory.setName(marketCategory.getName());
            newMarketCategory.setDesc(marketCategory.getDesc());
            newMarketCategory.setId(marketCategory.getId());
            newMarketCategory.setIconUrl(marketCategory.getIconUrl());
            newMarketCategory.setPicUrl(marketCategory.getPicUrl());
            newMarketCategory.setKeywords(marketCategory.getKeywords());
            newMarketCategory.setLevel(marketCategory.getLevel());

            int affectedRows = marketCategoryMapper.insertSelective(newMarketCategory);


            if(affectedRows==1){
                baseRespVo.setErrmsg("成功");
               // baseRespVo.setData(data);
            }
        }

        return baseRespVo;
    }

    /**
     * http://182.92.235.201:8083/admin/category/update
     * @param marketCategory
     * @return
     */
    @Override
    public BaseRespVo update(MarketCategory marketCategory) {

        //MarketCategoryExample example=new MarketCategoryExample();
       // MarketCategoryExample.Criteria criteria=example.createCriteria();
       // criteria.andDeletedEqualTo(false);
       // marketCategoryMapper.updateByExampleSelective(marketCategory,example);

        BaseRespVo<MarketCategory> baseRespVo = new BaseRespVo<>();
        marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);

        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    @Override
    public BaseRespVo delete(MarketCategory marketCategory) {
        BaseRespVo<MarketCategory> baseRespVo=new BaseRespVo<>();
        marketCategory.setDeleted(true);
        marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
//        marketCategoryMapper.deleteByPrimaryKey(marketCategory.getId());

        baseRespVo.setErrmsg("成功");
        baseRespVo.setErrno(0);
        return baseRespVo;
    }

    @Override
    public CommonData<MarketCategory> l1(BasePageInfo info) {
        MarketCategoryExample example=new MarketCategoryExample();

        List<MarketCategory> list=marketCategoryMapper.selectByExample(example);
        PageInfo<MarketCategory> pageInfo=new PageInfo<>(list);

        pageInfo.setList(list);

        return CommonData.data(pageInfo);

    }


}
